# scripts/benchmark.py
import asyncio
import httpx
import time


async def test_performance():
    """性能基准测试"""
    async with httpx.AsyncClient() as client:
        # 测试标准化接口
        start_time = time.time()
        requests = []
        for i in range(100):  # 模拟100个并发请求
            request = {
                "product_id": f"test_{i}",
                "original_title": "夏季新款连衣裙女装",
                "original_description": "这是一款漂亮的夏季连衣裙",
                "target_languages": ["en", "es"]
            }
            requests.append(
                client.post("http://localhost:8002/api/v1/product/standardize", json=request)
            )

        responses = await asyncio.gather(*requests)
        end_time = time.time()

        print(f"✅ 标准化接口 QPS: {100 / (end_time - start_time):.2f}")

        # 测试P99延迟
        latencies = []
        for i in range(10):
            start = time.time()
            await client.post("http://localhost:8002/api/v1/product/standardize", json=request)
            latencies.append((time.time() - start) * 1000)  # 转毫秒

        latencies.sort()
        p99 = latencies[int(0.99 * len(latencies))]
        print(f"✅ P99延迟: {p99:.2f}ms")


if __name__ == "__main__":
    asyncio.run(test_performance())